//
//  CCArmatureFactory.cpp
//  FlashAnimation
//
//  Created by chiawei.tseng on 12/12/24.
//
//

#include "cocos2d.h"
#include "CCArmatureFactory.h"
#include "CConnectionData.h"
#include "CArmature.h"

CCArmatureFactory::~CCArmatureFactory()
{
    mapSpriteBatchNode::iterator pi = m_pSpriteBatchNodeSet.begin();
    
    while (pi != m_pSpriteBatchNodeSet.end())
    {
        pi->second.pSpriteBatchNode->release();
    }
    
    m_pSpriteBatchNodeSet.clear();
}

CCArmature* CCArmatureFactory::CreateArmature(const char* pArmatureName , const char* pAnimationName,const char* pnlName)
{
    CCSpriteBatchNode *batchNode = CCSpriteBatchNode::create(pnlName);
    
    CCAssert(batchNode,"CCSpriteBatchNode is NULL");
       
    CCArmature* armature = CCArmature::create(pArmatureName, pAnimationName, batchNode);
    
    return armature;
}

void CCArmatureFactory::Init()
{
    
}

void CCArmatureFactory::ReSetSpriteBathNode(const char* PngName)
{
    string _pngName(PngName);
    
    if(m_pSpriteBatchNodeSet.count(PngName) > 0)
    {                
        m_pSpriteBatchNodeSet[_pngName].bIsAddChild = false;
    }
}

void CCArmatureFactory::ReSetAllSpriteBathNode()
{
    mapSpriteBatchNode::iterator pi = m_pSpriteBatchNodeSet.begin();
    
    while (pi != m_pSpriteBatchNodeSet.end())
    {
        pi->second.bIsAddChild = false;
    }
}

void CCArmatureFactory::UseSpriteBatchNode(CCNode* pNode, const char* PngName)
{
    string _pngName(PngName);
    
    if(m_pSpriteBatchNodeSet.count(_pngName) == 0)
        return ;
        
    if(m_pSpriteBatchNodeSet[_pngName].bIsAddChild == false)
    {
        m_pSpriteBatchNodeSet[_pngName].bIsAddChild = true;
        
        pNode->addChild(m_pSpriteBatchNodeSet[_pngName].pSpriteBatchNode);
    }
}

CCSpriteBatchNode* CCArmatureFactory::GetSpriteBatchNode(const char* PngName)
{
    string _pngName(PngName);
    
    if(m_pSpriteBatchNodeSet.count(_pngName) > 0)
    {
        //m_pSpriteBatchNodeSet[_pngName].bIsAddChild = true;
        
        return m_pSpriteBatchNodeSet[_pngName].pSpriteBatchNode;
    }
    
    return NULL;
}

void CCArmatureFactory::Register(const char* XmlName, const char* PngName , const char* PlsitName)
{
    
    // step 1: add your skeleton xml file
    CCConnectionData::sharedConnectionData()->addData(XmlName);
     
    // step2: add plist and png to cache
    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(PlsitName,PngName);
     
    // step 3: create a CCSpriteBatchNode
}

void CCArmatureFactory::UnRegister()
{
    
}





